package xyz.bali16.module.person.information.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.starblues.bootstrap.annotation.AutowiredType;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.bali16.application.core.utils.IDUtils;
import xyz.bali16.module.person.information.entity.Information;
import xyz.bali16.module.person.information.entity.UserInformation;
import xyz.bali16.module.person.information.mapper.UserInformationMapper;
import xyz.bali16.module.person.information.model.InformationAddParam;
import xyz.bali16.module.person.information.mapper.InformationMapper;

/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information> implements InformationService {
    private final InformationMapper informationMapper;
    private final UserInformationMapper userInformationMapper;

    public InformationServiceImpl(InformationMapper informationMapper,@AutowiredType(AutowiredType.Type.PLUGIN_MAIN) UserInformationMapper userInformationMapper) {
        this.informationMapper = informationMapper;
        this.userInformationMapper = userInformationMapper;
    }

    @Override
    public void addInformation(InformationAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "InformationAddParam 参数不能为为空");
        Information information = new Information();
        information.setId(IDUtils.uuid());
        BeanUtils.copyProperties(param, information);


        UserInformation userInformation = new UserInformation();
        userInformation.setUserId(param.getUserId());
        userInformation.setInformationId(information.getId());

        userInformationMapper.insert(userInformation);
        save(information);
        System.out.println(information);


    }

    @Override
    public void updateInformation(InformationAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "InformationAddParam 参数不能为为空");
        Information information = new Information();
        BeanUtils.copyProperties(param, information);
        Wrapper<UserInformation> wrapper = Wrappers.<UserInformation>lambdaQuery().eq(UserInformation::getUserId,param.getUserId());
        UserInformation userInformation = userInformationMapper.selectOne(wrapper);
        information.setId(userInformation.getInformationId());
        updateById(information);
    }

    @Override
    public void delete(String informationId) throws Exception {
        if (informationId == null) {
            return;
        }
        removeById(informationId);
    }
}
